import { Toast } from "vant";
import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      component: () => import("../pages/myHome/MyHome.vue"),
    },
    {
      path: "/home",
      component: () => import("../pages/myHome/MyHome.vue"),
    },
    {
      path: "/cart",
      component: () => import("../pages/myCart/MyCart.vue"),
      meta: { isAuth: true },
    },
    {
      path: "/order",
      component: () => import("../pages/myOrder/MyOrder.vue"),
      meta: {
        isAuth: true,
      },
    },
    {
      path: "/mine",
      component: () => import("../pages/mine/Mine.vue"),
      meta: { isAuth: true },
    },
    {
      path: "/store",
      component: () => import("../pages/myStore/MyStore.vue"),
    },
    {
      path: "/createorder",
      component: () => import("../pages/createOrder/CreateOrder.vue"),
      meta: { isAuth: true },
    },
    {
      path: "/address",
      component: () => import("../pages/address/Address.vue"),
      meta: {
        isAuth: true,
      },
    },
    {
      path: "/addressedit",
      component: () => import("../pages/AddressEdit/AddressEdit.vue"),
      meta: {
        isAuth: true,
      },
    },
    {
      path: "/login",
      component: () => import("../pages/login/login.vue"),
    },
    {
      path: "/register",
      component: () => import("../pages/register/register.vue"),
    },
    {
      path: "/userinfoedit",
      component: () => import("../pages/userInfoEdit/userinfoedit.vue"),
    },
  ],
});

// 路由的前置守卫
router.beforeEach((to, from, next) => {
  if (to.meta.isAuth) {
    if (localStorage.isLogin === "1") {
      next();
    } else {
      Toast("请先登陆");
      router.push("./login");
    }
  } else {
    next();
  }
});

export default router;
